Skip to content

Adding KV-Cache Support to Flux2.Klein Image Editing - #465

Open
amepas wants to merge 2 commits into
mainfrom
onboarding-imageedit-kv-flux2klein
Open

Adding KV-Cache Support to Flux2.Klein Image Editing#465
amepas wants to merge 2 commits into
mainfrom
onboarding-imageedit-kv-flux2klein

Conversation

@amepas

@amepas amepas commented Aug 19, 2026

Copy link
Copy Markdown
Collaborator

Summary

Speeding up Image Editing on Flux2.Klein models by onboarding Flux2.Klein-9B-KV model variant. This is a separate model (same architecture, different weights) that was trained to support caching KV for reference images during image editing.

Builds on PR 461 so some overlapping code changes are included in this PR.

Usage

Just adding the use_kv=True flag to the 9B model call will switch to the KV-cache model support.

python generate_flux2klein.py
  prompt="add a bunch of animals swimming in the water in front of the castle and get rid of all birds" \
  image_paths="['/path/to/img1.png', '/path/to/img2.png']" \
  use_kv=True

Speed-Ups

Block-sizes can be tuned further for each setting. Using default settings still gives substantial improvements

Reference Images ($1024 \times 1024$) Latency Speedup
1 Image 1.28x
2 Images 1.32x
4 Images 1.74x

Visual Verification

Prompt: change the painting so she is facing forward instead of looking over her shoulder

Original:
image

Edited:
image

Correctness

KV-Cache implementation does not cause any regression or change in behavior on other flux2.klein pipelines.
Below numbers generated on v6-4.

  • 4B T2I Smoke Test: 0.982 SSIM from baseline
  • 9B T2I Smoke Test: 0.881 SSIM from baseline
  • 4B Image Edit Smoke Test: 0.996 from baseline
  • 9B Image Edit Smoke Test: 0.997 from baseline
  • 9B KV-Cache Image Edit Smoke Test: 0.999 from baseline
  • Diffusers KV-Cache Image Edit E2E Parity Test: 0.889 from PyTorch Diffusers

Note: KV-Cache requires a separate model checkpoint/training setup and is only supported on the 9B model.

@github-actions

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces support for the Flux2Klein model, including new configuration files, NNX-based model implementations for the Transformer and VAE, and a dedicated pipeline. It also adds end-to-end parity and smoke tests. The review identified several critical issues: missing imports in flux2klein_pipeline.py and generate_flux2klein.py that will cause runtime errors, and the incorrect use of nnx.silu instead of jax.nn.silu across multiple model files.

Comment thread src/maxdiffusion/pipelines/flux/flux2klein_pipeline.py
Comment thread src/maxdiffusion/generate_flux2klein.py
Comment thread src/maxdiffusion/models/flux/transformers/transformer_flux_flax.py
Comment thread src/maxdiffusion/models/flux/transformers/transformer_flux_flax.py Outdated
Comment thread src/maxdiffusion/models/flux/transformers/transformer_flux_flax.py Outdated
Comment thread src/maxdiffusion/models/flux/vae/autoencoder_kl_flux2_nnx.py
Comment thread src/maxdiffusion/models/flux/vae/autoencoder_kl_flux2_nnx.py
Comment thread src/maxdiffusion/models/flux/vae/autoencoder_kl_flux2_nnx.py
…X (4B & 9B) with E2E parity and smoke tests

- Add multi-image editing pipeline (FlaxFlux2KleinPipeline) supporting arbitrary reference image conditioning
- Port VAE encoder and decoder to Flax NNX (AutoencoderKLFlux2NNX)
- Implement efficient image preprocessing, tiling, aspect-ratio-aware padding, and packing utilities
- Support fast sharded safetensors loading and TP sharding for Klein 4B & 9B
- Use standard HF_HOME resolution without machine-specific hardcoded paths
- Ensure clean compatibility across Transformers 4.x and 5.x via lazy module loading and dynamic FlaxPreTrainedModel lookup
- Add end-to-end multi-image editing parity test and preprocessing unit test suite
@amepas
amepas force-pushed the onboarding-imageedit-kv-flux2klein branch 2 times, most recently from 6238524 to f04bf0e Compare August 26, 2026 16:43
…age editing with E2E parity and smoke tests

- Implement prefix extraction phase (step 0) and cached denoising scan loop in FlaxFlux2KleinPipeline
- Support KV cache slicing and concatenation across Flax NNX Double and Single transformer blocks
- Safely bound Splash/Flash attention block sizes for asymmetric cross-attention sequences with opt-in flag
- Add CLI and config support for use_kv with dynamic FLUX.2-klein-9b-kv repository resolution
- Add cross-framework E2E parity test achieving 0.8881 SSIM / 22.58 dB PSNR
- Add 9B KV-cache image editing smoke test with verified golden reference image
@amepas
amepas force-pushed the onboarding-imageedit-kv-flux2klein branch from f04bf0e to af7a73f Compare August 26, 2026 16:45
@amepas
amepas marked this pull request as ready for review August 26, 2026 16:53
@amepas
amepas requested a review from entrpn as a code owner August 26, 2026 16:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant